package main

import (
	"crypto/rand"
	"fmt"
	"math/big"
	"strconv"
	"time"
)

func say(s string) {
	for i := 0; i < 5; i++ {
		time.Sleep(100 * time.Microsecond)
		fmt.Println(s)

	}
}
func sum(s []int, c chan int) {
	sum := 0
	for _, v := range s {
		sum += v
	}
	result, _ := rand.Int(rand.Reader, big.NewInt(128))
	number := result.String() //转成string
	num, _ := strconv.Atoi(number)
	fmt.Printf("%d\n", num)
	time.Sleep(time.Duration(num) * time.Microsecond)
	c <- sum
}
func main() {
	go say("world")
	say("hello")
	s := []int{7, 2, 8, -9, 4, 0}
	c := make(chan int)
	go sum(s[:len(s)/2], c)
	go sum(s[len(s)/2:], c)
	//先执行完先赋值
	x, y := <-c, <-c
	fmt.Println(x, y, x+y)
	ch := make(chan int, 2)
	ch <- 1
	ch <- 2
	fmt.Println(<-ch, <-ch)
}
